<!DOCTYPE HTML>
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
<script src='../../resources/gesture-util.js'></script>
<style>
  body,html {
    margin: 0;
    width: 10000px;
    height: 10000px;
  }
</style>

<script>
  const scroller = document.scrollingElement;
  let floatPrecision = 0.00001;

  window.addEventListener('load', async () => {
    await waitForCompositorCommit();

    promise_test(async () => {
      scroller.scrollTop = 0;
      scroller.scrollLeft = 0;
      const distance = 5;
      const x = 400;
      const y = 300;
      const precise_deltas = true;

      if (window.internals) {
        window.internals.setPageScaleFactor(4);
        window.internals.setVisualViewportOffset(600, 450);
        await waitForCompositorCommit();
      }

      let expected_scroll = distance / window.visualViewport.scale;

      // If fractional scroll offsets are enabled, the expected scroll offset
      // should be the actuall fractional distance we scrolled. Otherwise the
      // offset should be truncated.
      if (internals.runtimeFlags.fractionalScrollOffsetsEnabled) {
        assert_not_equals(expected_scroll,
                          Math.round(expected_scroll),
                          'Expected scroll should be fractional');
      } else {
        expected_scroll = Math.floor(expected_scroll);
      }

      // Scroll Vertically
      await smoothScroll(distance,
                         x, y,
                         GestureSourceType.TOUCHPAD_INPUT,
                         'down',
                         SPEED_INSTANT,
                         precise_deltas);
      assert_approx_equals(scroller.scrollTop, expected_scroll, floatPrecision);

      // Scroll Horizontally
      await smoothScroll(distance,
                         x, y,
                         GestureSourceType.TOUCHPAD_INPUT,
                         'right',
                         SPEED_INSTANT,
                         precise_deltas);
      assert_approx_equals(scroller.scrollLeft, expected_scroll, floatPrecision);
    }, 'Scrolling while zoomed scroller preserves fractional offsets.');
  });
</script>
